package com.lianxian.xindong.life.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.iarcuschin.simpleratingbar.SimpleRatingBar;
import com.lianxian.xindong.life.R;
import com.lianxian.xindong.life.R2;
import com.lianxian.xindong.life.activity.BusinessDetailActivity;
import com.lianxian.xindong.life.entities.FoodRankingContentModel;
import com.lianxian.xindong.life.util.GlideUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FoodRankingContentAdapter extends RecyclerView.Adapter {
    List<FoodRankingContentModel> list;
    Context context;

    public FoodRankingContentAdapter(List<FoodRankingContentModel> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.item_food_ranking_content, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        FoodRankingContentModel model =list.get(position);
        GlideUtil.GlideWithPlaceHolder(context, model.getPicUrl()).into(viewHolder.headPicIV);
        viewHolder.nameTV.setText(model.getName());
        viewHolder.ratingBar.setRating(model.getScore());
        viewHolder.priceTV.setText("¥"+model.getPrice()+"/人");
        viewHolder.scoreTV.setText(model.getScore()+"分");
        viewHolder.distanceTV.setText(model.getDistance()+"km");
        viewHolder.recommendReasonTV.setText(model.getRecommendReason());
        viewHolder.rankingTV.setText(model.getRanking()+"");
        if (model.getRanking()<=3){
            viewHolder.topLayout.setBackgroundResource(R.mipmap.rectangle_58);
        }else {
            viewHolder.topLayout.setBackgroundResource(R.mipmap.rectangle_58_2);

        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(),BusinessDetailActivity.class);
                v.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    class  ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R2.id.item_picture)
        ImageView headPicIV;
        @BindView(R2.id.name)
        TextView nameTV;
        @BindView(R2.id.ratingbar)
        SimpleRatingBar ratingBar;
        @BindView(R2.id.scroe_text)
        TextView scoreTV;
        @BindView(R2.id.price)
        TextView priceTV;
        @BindView(R2.id.distance)
        TextView distanceTV;
        @BindView(R2.id.recommend_reason)
        TextView recommendReasonTV;
        @BindView(R2.id.ranking_tv)
        TextView rankingTV;
        @BindView(R2.id.top_number_layout)
        LinearLayout topLayout;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);

        }
    }
}
